package lv

import (
	"xr-game-server/core/constants/common"
	"xr-game-server/core/event"
	"xr-game-server/dao/roledao"
	"xr-game-server/gamecfg"
	"xr-game-server/gameevent"
	"xr-game-server/module/protocol"
)

type ExpCardHandler struct {
	protocol.PropHandler
}

func (receiver *ExpCardHandler) Use(roleId uint64, item *protocol.ItemModel) {

	roleData := roledao.GetRoleByRole(roleId)
	old := roleData.Exp

	//道具使用参数格式:[来源id,经验id,参数]
	//经验卡,添加数值,应该根据itemcfg配置表的值,项目应该根据需求处理
	itemCfg := gamecfg.GetItemById(item.Id)
	param := itemCfg.GetPropParam()
	//添加经验
	add(roleId, param[2], true)

	//发送日志,经验id,需要项目处理
	event.Pub(gameevent.ItemChange, gameevent.NewItemChangeData(
		roleId,
		[]*protocol.ItemModel{
			&protocol.ItemModel{
				Id:     param[1],
				Val:    param[2],
				TypeId: common.Add,
				Source: &gamecfg.ItemSourceCfg{
					Id: param[0],
				},
				Old:     old,
				Current: roleData.Exp,
			}}))
}

func initPropHandler() {
	protocol.RegisterPropHandler(protocol.ExpCard, &ExpCardHandler{})
}
